package com.thrsdk.ui.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

import com.thrsdk.constants.AppConfig;


public class WebDialog extends Dialog implements View.OnClickListener {
    private Context mContext;

    private Webdialoglistener webdialoglistener;
    private View mView;
    private Button btn_confirm;
    private Button btn_cancle;
    private WebView thf_web;
    private String url;

    public void setUrl(String url) {
        this.url = url;
    }

    public WebDialog(Context context) {
        super(context, AppConfig.resourceId(context,
                "thf_MyDialog", "style"));
        this.mContext = context;
        this.mView = LayoutInflater.from(context).inflate(
                AppConfig.resourceId(context, "thf_web_dialog", "layout"),
                null);


    }

    @Override
    public void show() {

        super.show();
        this.thf_web.loadUrl(this.url);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mView);
        thf_web = (WebView) mView.findViewById(AppConfig.resourceId(mContext, "thf_web", "id"));
        btn_confirm = (Button) mView.findViewById(AppConfig.resourceId(mContext, "btn_confirm", "id"));
        btn_cancle = (Button) mView.findViewById(AppConfig.resourceId(mContext, "btn_cancle", "id"));
        btn_confirm.setOnClickListener(this);
        btn_cancle.setOnClickListener(this);
        initWebSetting();

        this.setCancelable(false);
        this.setCanceledOnTouchOutside(false);

    }

    private void initWebSetting() {
        WebSettings settings = thf_web.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setSupportZoom(true);
        settings.setDomStorageEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setAllowFileAccess(true);// 设置允许访问文件数据
        settings.setUseWideViewPort(true);
        settings.setSupportMultipleWindows(true);
        settings.setBlockNetworkImage(false);//同步请求图片
        thf_web.setVerticalScrollBarEnabled(true);
        thf_web.setHorizontalScrollBarEnabled(true);

        thf_web.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }
        });
        thf_web.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) { //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
                view.loadUrl(url);
                return false;
            }

            //重写此方法可以让webview处理https请求。
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                try {
                    AlertDialog.Builder builder = new AlertDialog.Builder(thf_web.getContext());
                    builder.setMessage("SSL认证失败，是否继续访问？");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            handler.proceed();// 接受https所有网站的证书
                        }
                    });

                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            handler.cancel();
                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();
                } catch (Throwable t) {
                    handler.cancel();
                }
            }

            //在加载页面资源时会调用，每一个资源（比如图片）的加载都会调用一次。
            @Override
            public void onLoadResource(WebView view, String url) {
                super.onLoadResource(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
//                showDialogLoading();
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
//                hideDialogLoading();
                super.onPageFinished(view, url);
            }
        });


    }

    @Override
    public void onClick(View view) {
        this.dismiss();
        if (view == btn_confirm) {
            if (null != webdialoglistener) {
                webdialoglistener.onConfirm();
            }

        } else if (view == btn_cancle) {
            if (null != webdialoglistener) {
                webdialoglistener.onCancle();
            }
        }
    }

    public interface Webdialoglistener {
        void onConfirm();

        void onCancle();

    }

    public void setWebdialoglistener(Webdialoglistener listener) {
        this.webdialoglistener = listener;
    }
}
